package com.c.ctgproject.adapter.guang

import android.content.Context
import android.text.Editable
import android.text.TextWatcher
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.EditText
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.c.ctgproject.R
import com.c.ctgproject.base.BaseEmptyRecyclerViewAdapter
import com.c.ctgproject.base.BaseEmptyRecyclerViewAdapterV2
import com.c.ctgproject.base.BaseRecyclerViewAdapter
import com.c.ctgproject.entity.guang.CarPartHandlerEntity
import com.c.ctgproject.entity.guang.CarPartHandlerEntityV2
import com.c.ctgproject.utils.handlerNull
import com.c.ctgproject.utils.handlerText
import com.c.ctgproject.utils.textSet

class CarPartHandlerChildAdapter(private val parentPosition: Int, private val context: Context, private val mList: List<CarPartHandlerEntityV2.DataBean.PartsListBean>, private val listener: CarPartHandlerChildAdapterListener) : BaseRecyclerViewAdapter(context, mList) {
    override fun createViewHolder(parent: ViewGroup): RecyclerView.ViewHolder {
        val view = LayoutInflater.from(context).inflate(R.layout.item_car_part_handler_child_list, parent, false)
        return MyViewHolder(view)
    }

    override fun handlerData(holder: RecyclerView.ViewHolder, position: Int) {
        if (holder is MyViewHolder) {
            val data = mList[position]
            "配件名:${data.partsName.handlerNull()}".also { holder.mPartNameTV.text = it }
            holder.mPartCountET.textSet(data.partsCount.handlerNull())
            holder.mPartCountET.handlerText {
                if (holder.mPartCountET.hasFocus()) {
                    val num: Int = if (it.isEmpty()) {
                        0
                    } else {
                        it.toInt()
                    }
                    data.partsCount = num.handlerNull()
                }
            }

            val watcher = object : TextWatcher {
                override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

                }

                override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {

                }

                override fun afterTextChanged(s: Editable?) {
                    if (holder.mPartCountET.hasFocus()) {
                        //判断当前EditText是否有焦点在
                        listener.onNumChange(s.toString(), parentPosition, position)
                    }
                }
            }
            holder.mPartCountET.setOnFocusChangeListener { _, hasFocus ->
                if (hasFocus) {
                    holder.mPartCountET.addTextChangedListener(watcher)

                } else {
                    holder.mPartCountET.removeTextChangedListener(watcher)
                }
            }
        }
    }

    interface CarPartHandlerChildAdapterListener {
        fun onNumChange(str: String, parentPosition: Int, position: Int)
    }

    inner class MyViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val mPartNameTV: TextView = view.findViewById(R.id.mPartNameTV)
        val mPartCountET: EditText = view.findViewById(R.id.mPartCountET)
    }
}